﻿using DevExpress.Web.ASPxGridView;
using GPSTracker.Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web.UI;

namespace GPSTracker.Views.Report
{
    public partial class DeliveryStatus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                }
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "Delivery Status", "Page_Load");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
            }
        }

        protected void detailGrid_DataSelect(object sender, EventArgs e)
        {
            try
            {
                Session["DeliveryEntryID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "Delivery Status", "detailGrid_DataSelect");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
            }            
        }

        protected void grdView_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e)
        {
            try
            {
                var grid = (ASPxGridView)sender;
                var keyValue = grid.GetRowValues(e.VisibleIndex, "DeliveryEntryID").ToString();
                //hdnAreaID.Value = keyValue;

                if (e.ButtonID == "btnView")
                {
                    this.ImgCustPhoto.ImageUrl = "ImageURL.aspx?DeID=" + keyValue + "&CallFrom=C1";
                    this.ImgCustSing.ImageUrl = "ImageURL.aspx?DeID=" + keyValue + "&CallFrom=C2";
                    this.txtFeedback.Value = this.GetFeedback(keyValue, "C3");

                    //txtAreaName.Focus();
                    //btnUpdate.Visible = true;
                    //btnSave.Visible = false;
                    //lblMessage.Visible = false;
                    //lblMessage.Text = string.Empty;
                    //ClearPopUpCntrls(true);
                    
                    popup.ShowOnPageLoad = true;

                    //var dt = new DataTable();
                    //dt = objAreaDAL.FillAreaMasterGrid(keyValue);
                    //if (dt.Rows.Count > 0)
                    //{
                    //    txtAreaName.Text = dt.Rows[0]["AreaName"].ToString();
                    //    txtAreaDesc.Text = dt.Rows[0]["AreaDesc"].ToString();
                    //}
                }
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "Delivery Status", "grdView_CustomButtonCallback");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
            }
        }

        private string GetFeedback(string DeID, string callFrom)
        {
            try
            {
                var cmd = new SqlCommand();
                cmd.Connection = CommonFx.GetSqlCon();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SP_GetDeliveryStatusByDeID";

                cmd.Parameters.Add(new SqlParameter("@pDeID", DeID));
                cmd.Parameters.Add(new SqlParameter("@pCallFrom", callFrom));

                cmd.Parameters.Add("@pFeedback", SqlDbType.NVarChar, 200000000);

                cmd.Parameters.Add("@pIsErrorFound", SqlDbType.Bit);
                cmd.Parameters.Add("@pErrorMessage", SqlDbType.NVarChar, 4000);
                
                cmd.Parameters["@pFeedback"].Direction = ParameterDirection.Output;

                cmd.Parameters["@pIsErrorFound"].Direction = ParameterDirection.Output;
                cmd.Parameters["@pErrorMessage"].Direction = ParameterDirection.Output;

                cmd.ExecuteNonQuery();

                if (!Convert.ToBoolean(cmd.Parameters["@pIsErrorFound"].Value))
                {
                    object obj = cmd.Parameters["@pFeedback"].Value;

                    if (!String.IsNullOrEmpty(obj.ToString()))
                        return obj.ToString();
                    else
                        return "NOT AVAILABLE";
                }
                else
                {
                    return "NOT AVAILABLE";
                }
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "DeliveryStatus", "GetFeedback");
                return "NOT AVAILABLE";
            }
        }
    }
}
